import java.util.Arrays;

public class J2411121 {
    public int[] elem;
    public int usedSize;//数组中的有效数字个数

    public static final int DEFAULT_SIZE = 4;

    public J2411121() {
        this.elem = new int[DEFAULT_SIZE];
    }

    public void display() {     //获取数组中的有效数据
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(this.elem[i] + " ");
        }
    }

    public void add(int data) {  //添加数组中的数据
        if (ifFulls()) {   //如果数组已满则进行扩容
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    public void add1(int pos, int data) {  //在数组中插入数值
        if (pos < 0 || pos > this.usedSize) {
            throw new PosOutOfBoundsException("位置不合法");
//            System.out.println("位置不合法");
//            return;
        }
        if (ifFulls()) {   //如果数组已满则进行扩容
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.elem[i + 1] = this.elem[i];
            this.elem[pos] = data;
            this.usedSize++;
        }
    }

    public boolean ifFulls() {
        if (this.usedSize == this.elem.length) {
            return true;
        }
        return false;
    }

    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                System.out.println(toFind);
                return true;
            }
            return false;
        }
        return false;
    }

    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                System.out.println(i);
                return i;
            }
        }
        return -1;
    }

    public int get(int pos) {  //根据下标找数剧
        if (pos < 0 || pos >= this.usedSize) {
            throw new PosOutOfBoundsException("位置不合法");
        }
        return elem[pos];
    }

    public int set(int pos, int data) {
        if (pos < 0 || pos >= this.usedSize) {
            throw new PosOutOfBoundsException("位置不合法");
        }
        this.elem[pos] = data;
        return elem[pos];
    }
    public int size(){  //查找长度
        return this.usedSize;
    }
    public void remove(int toRemove){ //删除
        int val= indexOf(toRemove);
        if (indexOf(toRemove) == -1){
            System.out.println("没有这个数据");
        }
        for (int i = 0; i < this.usedSize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }
    public void clear(){  //清空表
        this.usedSize = 0;
    }
}